﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Management
{
    public partial class EditForm : Form
    {
        public EditForm()
        {
            InitializeComponent();
        }
        public EditForm(int id,string title,string author)
        {
            InitializeComponent();
            this.id = id;
            textBox1.Text=title;
            textBox2.Text=author;
        }
        private int id;
        private void button1_Click(object sender, EventArgs e)
        {
            var title = textBox1.Text;
            var author = textBox2.Text;

            if(id>0)
            {
                string cmdSting =string.Format( "update Articles set Title = '{0}',Author = '{1}' where Id = {2}",title,author,id);
                var upd = DbHelper.AddOrUpdateOrDelete(cmdSting);
                if(upd==1)
                {
                    MessageBox.Show("更新成功啦!", "提示");
                    this.DialogResult = DialogResult.Yes;
                    this.Close();
                }
            }
            else
            {
                string cmdSting = string.Format("insert into Articles(Title, Author)values('{0}', '{1}')", title, author);
                var add = DbHelper.AddOrUpdateOrDelete(cmdSting);
                if(add==1)
                {
                    MessageBox.Show("添加成功啦!", "提示");
                    this.DialogResult = DialogResult.Yes;
                    this.Close();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }
    }
}
